# -*- coding: utf8 -*-
# Written by koya and MMCOWBOY
import pygame, random, math
from pygame.locals import *

import image
import collidet

#Particle为粒子,初始化包括位置,最大速度,粒子的图片名,是否旋转

class particle_obj( pygame.sprite.Sprite ):

      def __init__( self, pos, type = 'Ellipse', range = 4, image_name = None, is_rotate = False, ):
            super( particle_obj, self ).__init__( self.containers )
            #image_name为None表示没有指定粒子图片,采用默认的粒子图片
            
            #print 'p_o init!'
            
            if type == 'Image':
                if image_name is None:
                    type = 'Ellipse'
                else:
                    self.image = load_image( image_name, "data", False)
            if type == 'Ellipse':
                size = random.randrange( 2, 10 )
                self.image = pygame.Surface( (size, size) )
                pygame.draw.ellipse( self.image, random.choice( [ (255, 255, 255),\
                                   (255, 255, 0), (255, 200, 0) ] ),[0,0,size,size] )
            if type == 'Line':
                self.image = pygame.Surface( (1, 8) )
                pygame.draw.line( self.image,  random.choice( [ (255, 255, 255),
                              (255, 255, 0), (255, 200, 0) ] ), (0,0), (0,8))
            if type == 'Spot':
                #print 'p_o spot!'
                self.image = pygame.Surface( (1,1) )
                pygame.draw.ellipse( self.image,  random.choice( [ (255, 255, 255),
                              (255, 255, 0), (255, 200, 0) ] ), [0,0,1,1])
                #print 'p_o spot~'
                              
            if type == 'Polygon':
                pass
            #print 'JJ ..!!'
            self.image.set_colorkey((0,0,0), pygame.RLEACCEL)
            #print 'JJ ..!!'
            self.image.convert()
            #print 'JJ ..!!'
            self.rect = self.image.get_rect( center = pos )
            #print 'JJ ..!!'
            self.vx = random.randrange( -range, range )
            #print 'JJ ..!!'
            self.vy = random.randrange( -range, range )
            #print 'JJ ..1!!'
            self.angle = -collidet.angle_to_degree(collidet.get_angle(self.vx, self.vy))
            #print 'JJ ..2!!'
            center = self.rect.center
            #print 'JJ ..3!!', self.angle
            if self.angle:
                self.image = pygame.transform.rotozoom(self.image, self.angle, 1.0)
            #print 'JJ ..4!!'
            self.rect = self.image.get_rect()
            #print 'JJ ..5!!'
            self.rect.center = center
            #print 'JJ ..6!!'
            self.life = 255
            #用于粒子的旋转
            #print 'JJ ..7!!'
            self.is_rotate = is_rotate
            #print 'JJ ..8!!'
            self.angle = random.randrange( -180, 180 )
            #print 'JJ ..9!!'
            self.origin_image = self.image
            #print 'JJ ..10!!'
            self.fade_speed = 5
            #print 'p_o init~'
            
      def update( self ):
            self.life -= self.fade_speed
            if self.life <= 0:
                  self.kill()
            if self.is_rotate:
                  self.angle += 1
                  center = self.rect.center
                  self.image = pygame.transform.rotozoom( self.origin_image, self.angle, 1.0 )
                  self.rect = self.image.get_rect()
                  self.rect.center = center
            self.image.set_alpha( self.life )
            self.rect.move_ip( self.vx, self.vy )

      def draw( self, surface ):
            surface.blit( self.image, self.rect )
            
      def destroy( self ):
            pygame.sprite.Sprite.kill( self )

class flame_spot(pygame.sprite.Sprite):

      def __init__(self, pos, x_speed = -1, y_speed = -1, size = -1, fade_speed = 5, range = 5):
            super(flame_spot, self).__init__(self.containers)
            #print 'f_s init!'
            if (size == -1):
                  self.size = random.randrange(1, 10)
            else:
                  self.size = size
            self.image = pygame.Surface((self.size, self.size))
            pygame.draw.ellipse(self.image, random.choice([(255, 244, 147),
                                                           (254, 228, 3), 
                                                           (167, 150, 1),
                                                           (251, 86, 15),
                                                           (253, 172, 138),
                                                           (255, 255, 255)]),
                                [0,0,self.size,self.size])
            self.image.set_colorkey((0,0,0), pygame.RLEACCEL)
            self.image.convert()
            self.rect = self.image.get_rect(center = pos)
            if (x_speed == -1 or y_speed == -1):
                  self.vx = random.randrange(-range, range)
                  self.vy = random.randrange(-range, range)
            else:
                  self.vx = x_speed
                  self.vy = y_speed
            self.trans = 255
            self.fade_speed = fade_speed
            #print 'f_s init~'

      def draw(self, screen):
            screen.blit(self.image, self.rect)

      def update(self):
            #print 'f_s update!'
            self.trans -= self.fade_speed
            if self.trans <= 0:
                  self.kill()
            self.image.set_alpha(self.trans)
            self.rect.move_ip(self.vx, self.vy)
            #print 'f_s update~'

      def destroy(self):
            #print 'f_s destroy!'
            pygame.sprite.Sprite.kill(self)
            #print 'f_s destroy~'
            
class shock_wave(pygame.sprite.Sprite):

    def __init__(self, pos, type = 'Ellipse', range = 1, speed = 2, degrees = 360):
        super(shock_wave, self).__init__(self.containers)
        self.particles = pygame.sprite.Group()
        particle_obj.containers = self.particles
        for d in xrange(degrees):
            sx,sy = collidet.get_offsets(speed, d * 360.0 / degrees)
            p_obj = particle_obj( pos, type, range)
            p_obj.vx = sx * range
            p_obj.vy = sy * range
            p_obj.fade_speed = 4
        spark_obj(pos, 'Line', 20, 5)
        spark_obj(pos, 'Ellipse', 20, 5)
        
    def update( self ):
        for particle in self.particles:
            particle.update()
        
    def draw( self, surface ):
        for particle in self.particles:
            surface.blit( particle.image, particle.rect )
        


class spark_obj( pygame.sprite.Sprite ):

      
      def __init__( self, pos, type = 'Ellipse', particle_num = 30, range = 5,\
                    image_name = None, is_rotate = False  ):
            super( spark_obj, self ).__init__( self.containers )
            #print 's_o init!'
            self.particles = pygame.sprite.Group()
            #print 'JJ: ..!!'
            particle_obj.containers = self.particles
            #print 'JJ: ...!!'
            for i in xrange( particle_num ):
                  #print i," "
                  particle_obj( pos, type, range, image_name, is_rotate)
            #print 's_o init!'

      def update( self ):
            for particle in self.particles:
                  particle.update()

      def draw( self, surface ):
            for particle in self.particles:
                  surface.blit( particle.image, particle.rect )
            
       
class explosition( pygame.sprite.Sprite ):
      def __init__( self, pos, color, max_size ):
            super( explosition, self ).__init__( self.containers )
            self.image = pygame.Surface( ( 0, 0 ) )
            self.rect = self.image.get_rect( center = pos )
            self.color = color 
            self.pos = pos
            self.radian = 1.0
            self.max_size = max_size
            self.circle_width = 0

      def update( self ):
           if self.radian <= self.max_size / 2.0:
               self.image = pygame.Surface( ( self.radian*2, self.radian*2 ) ).convert()
               self.image.set_colorkey( (0,0,0) )
               self.rect = self.image.get_rect( center = self.pos )
               pygame.draw.circle( self.image, self.color, self.pos, self.radian, 1 )
               self.radian += 10
           if self.radian > 1.0:
               self.circle_width = 1.0

      def draw( self, surface ):
          surface.blit( self.image, self.pos )